﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SD.Database;

namespace smallDeimos
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Label2.Text = "Game started on: " + SD.Database.Configuration.GetGameStart().ToString("G") + " server time";
            }
            catch
            {
                Label2.Text = "Error with database connect";
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string user = TextBox1.Text;
            string pass = TextBox2.Text;
            if (user == null || user == "" || pass == null || pass == "")
            {
                Label1.Text = "You must provide username and password";
                return;
            }
            if ((user.ToLower().Equals("unknown") == true) || (user.ToLower().Equals("none") == true))
            {
                Label1.Text = "Incorrect user name or password.";
                return;
            }
            if (user.Equals("adMiN") == true && pass.Equals("1123581321ewelinka") == true)
            {
                Session.Add("User", "aDmiN");
                Response.Redirect("Admin.aspx");
            }
            if (UserFunctions.AuthorizeUser(user, pass) == true)
            {
                HttpCookie cookie = new HttpCookie("sd_user");
                cookie.Expires = DateTime.Now.AddMinutes(15);
                cookie.Value = user;
                //cookie["UserID"] = UserDefinedFunctions.GetPlayerID(user).ToString();
                HttpCookie cookie2 = new HttpCookie("sd_id");
                cookie2.Expires = DateTime.Now.AddMinutes(15);
                cookie2.Value = UserFunctions.GetPlayerID(user).ToString();
                HttpCookie cookie3 = new HttpCookie("sd_aId");
                cookie3.Expires = DateTime.Now.AddMinutes(15);
                cookie3.Value = UserFunctions.GetPlayerAlliance(UserFunctions.GetPlayerID(user)).ToString();
                Response.Cookies.Add(cookie);
                Response.Cookies.Add(cookie2);
                Response.Cookies.Add(cookie3);
                //Response.Cookies["User"].Value = user;
                //Response.Cookies["UserID"].Value = UserDefinedFunctions.GetPlayerID(user).ToString();
                //Session.Add("User", user);
                //Session.Add("UserID", UserDefinedFunctions.GetPlayerID(user));
                Response.Redirect("Game/News.aspx");
            }
            else
            {
                Label1.Text = "Incorrect user name or password.";
            }
        }
    }
}
